#include "handler/command.h"

#include <string.h>
#include <stdio.h>

Command_t parseCommand(const char *input)
{
    Command_t command;
    memset(&command, 0, sizeof(command)); // 避免无参导致结构体成员随机值
    char cmd[10];                         // 预计指令长度小于10

    // 读取参数
    sscanf(input, "%10s %255[^\n ] %255[^\n]", cmd, command.arg1, command.arg2); // 中间255后的空格必须留着
    strcpy(command.name, cmd);

    if (strcmp(cmd, "login") == 0)
    {
        command.type = CMD_LOGIN;
    }
    else if (strcmp(cmd, "signup") == 0)
    {
        command.type = CMD_SIGNUP;
    }
    else if (strcmp(cmd, "quit") == 0)
    {
        command.type = CMD_QUIT;
    }
    else if (strcmp(cmd, "ls") == 0)
    {
        command.type = CMD_LS;
    }
    else if (strcmp(cmd, "ll") == 0)
    {
        command.type = CMD_LL;
    }
    else if (strcmp(cmd, "cd") == 0)
    {
        command.type = CMD_CD;
    }
    else if (strcmp(cmd, "pwd") == 0)
    {
        command.type = CMD_PWD;
    }
    else if (strcmp(cmd, "remove") == 0)
    {
        command.type = CMD_REMOVE;
    }
    else if (strcmp(cmd, "mv") == 0)
    {
        command.type = CMD_MV;
    }
    else if (strcmp(cmd, "mkdir") == 0)
    {
        command.type = CMD_MKDIR;
    }
    else if (strcmp(cmd, "download") == 0)
    {
        command.type = CMD_DOWNLOAD;
    }
    else if (strcmp(cmd, "upload") == 0)
    {
        command.type = CMD_UPLOAD;
    }
    else
    {
        command.type = CMD_INVALID;
        strcpy(command.name, "invalid");
    }

    return command;
}

bool isValidCommand(CommandType_t cmd_type)
{
    switch (cmd_type)
    {
    case CMD_LOGIN:
    case CMD_SIGNUP:
    case CMD_QUIT:
    case CMD_LS:
    case CMD_LL:
    case CMD_CD:
    case CMD_PWD:
    case CMD_REMOVE:
    case CMD_MV:
    case CMD_MKDIR:
    case CMD_DOWNLOAD:
    case CMD_UPLOAD:
        return true;
    default:
        return false;
    }
}
